/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.view.instance;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import offset.nodes.client.model.Server;
import offset.nodes.client.tree.model.TreeNodePanel;
import offset.nodes.client.tree.model.TreeNodePanelContainer;
import offset.nodes.client.model.TreeNodeUserObject;
import offset.nodes.client.vdialog.model.instance.TemplateInstance;
import offset.nodes.client.vdialog.model.instance.VirtualFolderInstance;

/**
 *
 * @author  Walter Lütgenau
 */
public class TemplateInstancePanel extends javax.swing.JPanel implements TreeNodePanel {

    class TextFieldDocumentListener implements DocumentListener {

        public void changedUpdate(DocumentEvent e) {
            validateOkButton();
        }

        public void insertUpdate(DocumentEvent e) {
            validateOkButton();
        }

        public void removeUpdate(DocumentEvent e) {
            validateOkButton();
        }
    }
    TreeNodePanelContainer container = null;
    Server server;
    TextFieldDocumentListener textFieldDocumentListener = new TextFieldDocumentListener();
    TemplateInstance userObject;

    /** Creates new form FolderPanel
     * @param server the server model
     * @param types the node types repository
     */
    public TemplateInstancePanel(Server server) {
        this.server = server;
        initComponents();
    }

    private void validateOkButton() {
        boolean ok = true;
        if (nameTextField.getText() == null || nameTextField.getText().length() == 0 ) {
            ok = false;
        }

        container.enableOkButton(ok);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        folderNameButtonGroup = new javax.swing.ButtonGroup();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        nameTextField.getDocument().addDocumentListener(textFieldDocumentListener);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/vdialog/view/DialogBundle"); // NOI18N
        nameLabel.setText(bundle.getString("template.templateInstancePanel.name")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(nameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameLabel))
                .addContainerGap(209, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup folderNameButtonGroup;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    // End of variables declaration//GEN-END:variables
    public void setPanel(TreeNodeUserObject treeNodeUserObject) {
        if ( treeNodeUserObject == null || !(treeNodeUserObject instanceof TemplateInstance))
            return;
        userObject = (TemplateInstance) treeNodeUserObject;
        
        if ( userObject.getName() == null || userObject.getName().length() == 0 ) {
            nameTextField.setEnabled(false);
        }
        else {
            nameTextField.setText(userObject.getName());
        }
    }

    public TreeNodeUserObject getUserObject() {
        userObject.setName(nameTextField.getText());
        return userObject;
    }

    public void setContainer(TreeNodePanelContainer container) {
        this.container = container;
    }
}
